$(function() {
	var token = localStorage.phone;
	var gps_j = '';
	var gps_w = '';
	$.ajax({
		url: 'https://newapp.icandrive.cn/api/distence.php',
		type: 'post',
		dataType: 'json',
		data: {
			token: token
		},
		success: function(data) {
			if(data.status == 200) {
				$(data.list).each(function() {
					gps_j = this.gps_j;
					gps_w = this.gps_w;
					// 百度地图API功能
					var map = new BMap.Map("allmap");
					var point = new BMap.Point(gps_j, gps_w);
					map.centerAndZoom(point, 13);
					var marker = new BMap.Marker(new BMap.Point(gps_j, gps_w)); // 创建点
					var circle = new BMap.Circle(point, 5000, {
						fillColor: "blue",
						strokeWeight: 1,
						fillOpacity: 0.3,
						strokeOpacity: 0.3
					});
					map.addOverlay(circle);
					map.addOverlay(marker); //增加点
					map.addOverlay(circle); //增加圆
					var newGpsJ = gps_j;
					var newGpsW = gps_w;

					function showInfo(e) {
						map.clearOverlays();
						var new_point = new BMap.Point(e.point.lng, e.point.lat);
						var marker = new BMap.Marker(new_point); // 创建标注
						var circle = new BMap.Circle(new_point, 5000, {
							fillColor: "blue",
							strokeWeight: 1,
							fillOpacity: 0.3,
							strokeOpacity: 0.3
						});
						map.addOverlay(marker);
						map.addOverlay(circle); // 将标注添加到地图中
						map.panTo(new_point);
						newGpsJ = e.point.lng;
						newGpsW = e.point.lat;
					}
					map.addEventListener("click", showInfo);

					$('.back').on('click', function() {

						if(newGpsJ != gps_j || newGpsW != gps_w) {
							$.ajax({
								url: 'https://newapp.icandrive.cn/api/savedistence.php',
								type: 'post',
								dataType: 'json',
								data: {
									token: token,
									gps_j: newGpsJ,
									gps_w: newGpsW
								},
								success: function(data) {
									if(data.status == 200) {
										window.location.href = '../coach-I/coach-me.html';
									}
								},
								error: function() {
									layer.msg('系统繁忙', function() {
										history.go(0)
									}, 1000);
								}
							});
						} else {
							history.back();
						}
					})

				})
			} else {}
		},
		error: function() {
			layer.msg('系统繁忙', function() {
				history.go(0)
			}, 3000);
		}
	});

});
